<template>
	<view>
		<view style="padding: 20px;">
			<view style="border-bottom: 1px solid #666666;padding: 10px 0;">
				<view style="margin-top:10px">
					家用报警器Mac：{{alarmMac}}
				</view>
				<view style="margin-top:10px">
					燃气表Mac：{{devMac}}
				</view>
			</view>
			<view style="padding: 10px 0;">
				<view class="">
					报警器IMEI：
				</view>
				<view class="searchbox">

					<uni-easyinput placeholder="请输入内容" :inputBorder="true" v-model="alarmName" :clearable="true" />
					<text style="font-size: 24px;color: #007AFF;margin-left: 10px;" class="iconfont icon-saoyisao"
						@tap.stop="scandev(1)"></text>
				</view>
			</view>
			<view style="border-top: 2px solid #666666;padding: 10px 0;">
				<view class="">
					燃气表编号：
				</view>
				<view class="searchbox">

					<uni-easyinput placeholder="请输入内容" :inputBorder="true" v-model="devName" :clearable="true" />
					<text style="font-size: 24px;color: #007AFF;margin-left: 10px;" class="iconfont icon-saoyisao"
						@tap.stop="scandev(2)"></text>
				</view>
			</view>

			<view class="primaryBtn" @click="startClient">
				<text>开始配对</text>
			</view>
			
			<u-loading-icon :show="loading"></u-loading-icon>
			<view style="text-align: center;margin-top: 20px;">
				{{typetext}}
			</view>
		</view>

	</view>
</template>
<script>
	import {
		myrequestUrl
	} from '@/common/utils.js'
	export default {
		data() {
			return {
				alarmName: '',
				devName: '',
				alarmMac: '',
				devMac: '',
				alarmBlueData: {},
				devBlueData: {},
				connectNum: 0,
				typetext: '',
				loading: false,
				serviceId: '',
				characteristicId: '',
				nowstep: 0,
				sendnum: 0,
				dayCount: 0,
				count: 0,
				deviceType: 1,
				devType: [
					'', '燃气表', '电磁阀', '独立探头'
				],
				sendok: false,
				clientok: false,
				testok: false,
				mtunum: 240,
				timers: []
			}
		},
		onLoad(option) {
			this.sendok = false
			this.clientok = false
			this.testok = false
			this.deviceType = 1
			this.dayCount = 0
			this.count = 0
			this.sendnum = 0
			this.nowstep = 0
			this.typetext = ''
			this.alarmName = ''
			this.devName = ''
			this.alarmMac = ''
			this.devMac = ''
			this.alarmBlueData = {}
			this.devBlueData = {}
			this.connectNum = 0
			this.serviceId = ''
			this.characteristicId = ''
			this.timers = []
			// this.getBindNum()
		},
		methods: {
			getCRC(data) {
				data = data.replace(/\s/g, "");
				const len = data.length;
				if (!(len % 2 === 0)) {
					return "0000";
				}
				const num = len / 2;
				const para = new Uint8Array(num);
				for (let i = 0; i < num; i++) {
					const value = parseInt(data.substring(i * 2, 2 * (i + 1)), 16);
					para[i] = value;
				}
				return this.getCRCFromBytes(para);
			},

			getCRCFromBytes(bytes) {
				// CRC寄存器全为1
				let CRC = 0x0000ffff;
				// 多项式校验值
				const POLYNOMIAL = 0x0000a001;
				let i, j;
				for (i = 0; i < bytes.length; i++) {
					CRC ^= (bytes[i] & 0x000000ff);
					for (j = 0; j < 8; j++) {
						if ((CRC & 0x00000001) !== 0) {
							CRC >>= 1;
							CRC ^= POLYNOMIAL;
						} else {
							CRC >>= 1;
						}
					}
				}
				// 结果转换为16进制
				let result = CRC.toString(16).toUpperCase();
				if (result.length !== 4) {
					const sb = "0000";
					result = sb.substring(0, 4 - result.length) + result;
				}
				// 交换高低位
				return result.substring(2, 4) + result.substring(0, 2);
			},

			test() {
				var _this = this
				uni.showModal({
					title: '连接成功',
					content: '是否跳转到设备添加页面',
					success: function(res) {
						if (res.confirm) {
							var imei = _this.alarmName
							uni.navigateTo({
								url: `/homePages/installDevice/index?imei=${imei}`,
							})
							_this.alarmName = ''
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			yiHuo(content) {
				let a = 0;
				for (let i = 0; i < content.length / 2; i++) {
					a = a ^ parseInt(content.substring(i * 2, (i * 2) + 2), 16);
				}
				let result = a.toString(16);
				if (result.length === 1) {
					return "0" + result;
				} else {
					return result;
				}
			},

			gopage() {
				uni.redirectTo({
					url: `/homePages/installDevice/index`
				})
			},
			//保存绑定信息
			saveBind() {
				var _this = this
				let data = {
					gasMeterCode: this.devName,
					gasMeterMac: this.devMac,
					deviceMac: this.alarmMac,
					deviceCode: this.alarmName,
					deviceType: this.deviceType,
					deptId: uni.getStorageSync('userInfo').data.deptId,
				}
				this.$request({
					url: this.$api.saveblueBind,
					method: "POST",
					dataType: 'json',
					data,
					success: (res) => {
						console.log("输出设备res", res)
						_this.getBindNum()
					},
					fail: (err) => {
						console.log("123");
					}
				})
			},
			//获取绑定数量
			getBindNum() {
				var _this = this
				let data = {
					deviceType: this.deviceType,
					deptId: uni.getStorageSync('userInfo').data.deptId,
				}
				this.$request({
					url: this.$api.bluebindNum,
					method: "GET",
					dataType: 'json',
					data,
					success: (res) => {
						console.log("输出设备res", res)
						_this.dayCount = res.data.data.dayCount
						_this.count = res.data.data.count
					},
					fail: (err) => {
						_this.loading = false
						_this.typetext = '蓝牙连接失败，请重试'
						_this.$refs.uToast.show({
							type: 'default',
							message: "蓝牙连接失败，请重试",
						})
						console.log("123");
					}
				})
			},
			//确认连接后测试报警
			testalarm() {
				var cmd = 'AA55040B010601010001183627A5'
				let buffers = this.hexStringToArrayBuffer(cmd)
				var _this = this
				uni.writeBLECharacteristicValue({
					// 这里的 deviceId 需要在 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
					deviceId: _this.alarmMac,
					// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
					serviceId: _this.serviceId,
					// 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
					characteristicId: _this.characteristicId,
					// 这里的value是ArrayBuffer类型
					value: buffers,
					success(res) {
						console.log('writeBLECharacteristicValue success', res)
						// 蓝牙发送mac地址
					},
					complete() {
						let times = setTimeout(() => {
							if (!_this.testok) {
								if (_this.sendnum < 4) {
									_this.sendnum++
									console.log('重新测试关阀')
									_this.testalarm()
								} else {
									_this.loading = false
									_this.typetext = '蓝牙连接失败，请重试'
									uni.showToast({
										title: '蓝牙连接失败，请重试"',
										icon: 'none'
									});
								}
							}
						}, 2000)

						_this.timers.push(times)
					}
				})
			},
			//写入后读取状态
			readBlue() {
				var cmd = 'AA55040B01030138000385FA48A5'
				let buffers = this.hexStringToArrayBuffer(cmd)
				var _this = this
				uni.writeBLECharacteristicValue({
					// 这里的 deviceId 需要在 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
					deviceId: _this.alarmMac,
					// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
					serviceId: _this.serviceId,
					// 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
					characteristicId: _this.characteristicId,
					// 这里的value是ArrayBuffer类型
					value: buffers,
					success(res) {
						console.log('writeBLECharacteristicValue success', res)
						// 蓝牙发送mac地址
					},
					complete() {
						let times = setTimeout(() => {
							if (!_this.clientok) {
								if (_this.sendnum < 4) {
									_this.sendnum++
									console.log('重新读取状态')
									_this.readBlue()
								} else {
									_this.loading = false
									_this.typetext = '蓝牙连接失败，请重试'
									uni.showToast({
										title: '蓝牙连接失败，请重试"',
										icon: 'none'
									});
								}
							}
						}, 2000)
						_this.timers.push(times)
					}
				})
			},
			//写入蓝牙
			writeBlueVal(cmd) {
				// var cmd = 'AA55040B010301380001043B0AA5'
				let buffers = this.hexStringToArrayBuffer(cmd)
				var _this = this
				console.log('----------------')
				console.log(_this.serviceId, _this.characteristicId, cmd)
				console.log('----------------')
				uni.writeBLECharacteristicValue({
					// 这里的 deviceId 需要在 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
					deviceId: _this.alarmMac,
					// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
					serviceId: _this.serviceId,
					// 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
					characteristicId: _this.characteristicId,
					// 这里的value是ArrayBuffer类型
					value: buffers,
					success(res) {
						console.log('writeBLECharacteristicValue success', res)
						// 蓝牙发送mac地址
					},
					complete(res) {
						let times = setTimeout(() => {
							if (!_this.sendok) {
								if (_this.sendnum < 4) {
									_this.sendnum++
									console.log('重新写入')
									_this.getWriteData(_this.devMac)
								} else {
									_this.loading = false
									_this.typetext = '蓝牙连接失败，请重试'
									uni.showToast({
										title: '蓝牙连接失败，请重试"',
										icon: 'none'
									});
								}
							}
						}, 2000)
						_this.timers.push(times)
					}
				})
			},
			//后台获取写入的数据
			async getWriteData(mac) {
				var _this = this
				let str = mac.split(':').reverse().join('')
				let crcstr = '01100138000306' + str
				var crcValue = this.getCRC(crcstr);
				var yihuostr = '0412' + crcstr + crcValue
				const yihuoVal = this.yiHuo(yihuostr)
				var cmd = 'AA55041201100138000306' + str + crcValue + yihuoVal + 'A5'
				_this.sendok = false
				_this.writeBlueVal(cmd)

				// console.log(mac, str)
				// let val1 = parseInt('' + str[0] + str[1], 16)
				// let val2 = parseInt('' + str[2] + str[3], 16)
				// let val3 = parseInt('' + str[4] + str[5], 16)
				// let data = {
				// 	deviceCode: 1,
				// 	attributeFlag: 150,
				// 	operationType: 1,
				// 	infoType: 6,
				// 	moduleType: 4,
				// 	issuedValue: val1 + ',' + val2 + ',' + val3
				// }
				// console.log(data)
				// var res = await myrequestUrl({
				// 	url: `https://oda.chichengcloud.cn/oda/bluetoothDevice/queryBluetoothCmdList`,
				// 	data: data,
				// 	method: 'post'
				// })
				// if (res.code == 0) {
				// 	console.log("通过指令获取对象", res.data)
				// 	let cmd = res.data[0].bluetoothCmd
				// 	_this.sendok = false
				// 	_this.writeBlueVal(cmd)
				// } else {
				// 	console.log("123");
				// }


			},
			// 处理传输的十六进制数据
			hexStringToArrayBuffer(str) {
				// console.log(str)
				if (!str) {
					return new ArrayBuffer(0);
				}
				var buffer = new ArrayBuffer(str.length / 2);
				let dataView = new DataView(buffer)
				let ind = 0;
				for (var i = 0, len = str.length; i < len; i += 2) {
					let code = parseInt(str.substr(i, 2), 16)
					dataView.setUint8(ind, code)
					ind++
				}
				return buffer;
			},
			//蓝牙写入
			writeBlueBefore() {
				//获取蓝牙设备服务信息
				var _this = this
				uni.getBLEDeviceServices({
					// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
					deviceId: _this.alarmMac,
					success(res) {
						console.log('device services:', res.services)
						for (let item of res.services) {
							if (item.uuid.indexOf('FE60') != -1) {
								_this.serviceId = item.uuid
								break;
							}
						}
						//获取蓝牙设备某个服务中所有特征值(characteristic)。
						uni.getBLEDeviceCharacteristics({
							// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
							deviceId: _this.alarmMac,
							// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
							serviceId: _this.serviceId,
							success(res) {
								console.log('device getBLEDeviceCharacteristics:', res
									.characteristics)
								for (let item of res.characteristics) {
									if (item.uuid.indexOf('FE63') != -1) {
										_this.characteristicId = item.uuid
										break;
									}
								}
								//监听特征值状态变化
								uni.notifyBLECharacteristicValueChange({
									state: true, // 启用 notify 功能
									// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
									deviceId: _this.alarmMac,
									// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
									serviceId: _this.serviceId,
									// 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
									characteristicId: _this.characteristicId,
									success(res) {
										console.log('监听特征值状态变化', res)
										_this.nowstep = 1
										_this.getWriteData(_this.devMac)
										//特征值状态变化触发事件
										uni.onBLECharacteristicValueChange(function(res) {
											var val = (_this.ab2hex(res.value) + '')
												.toUpperCase()
											console.log('特征值变化', val, _this.nowstep)
											if (_this.nowstep == 1) {
												//监听蓝牙返回的数据
												if (val ==
													'55AA040B0110013800030039195A') {
													_this.sendok = true
													_this.clientok = false
													console.log('MAC地址写入成功')
													_this.nowstep = 2
													_this.readBlue()
													_this.sendnum = 0
												} else {
													if (_this.sendnum < 4) {
														_this.sendnum++
														_this.getWriteData(_this.devMac)
													} else {
														_this.loading = false
														_this.typetext = '蓝牙连接失败，请重试'
														uni.showToast({
															title: '蓝牙连接失败，请重试"',
															icon: 'none'
														});
													}
												}
											} else if (_this.nowstep == 2) {
												//读取配对的燃气表是否正确
												let backstr = val.slice(14, 26)
												console.log(val, backstr, (_this.devMac
														.split(':')
														.reverse().join(''))
													.toUpperCase(), '-----------')
												if (backstr == (_this.devMac.split(':')
														.reverse().join(''))
													.toUpperCase()) {
													console.log('读取成功')
													_this.clientok = true
													_this.testok = false
													_this.sendnum = 0
													_this.nowstep = 3
													_this.testalarm()
												} else {
													if (_this.sendnum < 4) {
														_this.sendnum++
														_this.readBlue()
													} else {
														_this.loading = false
														_this.typetext = '蓝牙连接失败，请重试'
														uni.showToast({
															title: '蓝牙连接失败，请重试"',
															icon: 'none'
														});
													}
												}
											} else if (_this.nowstep == 3) {
												if (val ==
													'55AA040B0106010100011836235A'
												) {
													console.log('测试报警成功')
													_this.testok = true
													_this.loading = false
													_this.typetext = '蓝牙连接成功'
													_this.timers.forEach(timerId => clearTimeout(timerId))
													_this.timers = []
													// 页面卸载时停止蓝牙搜索
													uni.closeBluetoothAdapter();
													// 移除蓝牙设备发现监听
													uni.offBluetoothDeviceFound();
													// 移除特征值变化监听
													uni.offBLECharacteristicValueChange();
													// _this.saveBind()
													uni.showModal({
														title: '连接成功',
														content: '是否跳转到设备添加页面',
														success: function(res) {
															if (res.confirm) {
																var imei =
																	_this
																	.alarmName
																uni.navigateTo({
																	url: `/homePages/installDevice/index?imei=${imei}`
																})
																_this.sendok =
																	false
																_this
																	.clientok =
																	false
																_this.testok =
																	false
																_this
																	.deviceType =
																	1
																_this
																	.dayCount =
																	0
																_this.count = 0
																_this.sendnum =
																	0
																_this.nowstep =
																	0
																_this
																	.typetext =
																	''
																_this
																	.alarmName =
																	''
																_this.devName =
																	''
																_this
																	.alarmMac =
																	''
																_this.devMac =
																	''
																_this
																	.alarmBlueData = {}
																_this
																	.devBlueData = {}
																_this
																	.connectNum =
																	0
																_this
																	.serviceId =
																	''
																_this
																	.characteristicId =
																	''
															} else if (res
																.cancel) {
																console.log(
																	'用户点击取消'
																);
															}
														}
													});
													uni.closeBluetoothAdapter({
														success(res) {
															console.log(res)
														}
													})
													// 移除蓝牙设备发现监听
													uni.offBluetoothDeviceFound();

													// 移除特征值变化监听
													uni.offBLECharacteristicValueChange();
												} else {
													if (_this.sendnum < 4) {
														_this.sendnum++
														_this.testalarm()
													} else {
														_this.loading = false
														_this.typetext = '蓝牙连接失败，请重试'
														uni.showToast({
															title: '蓝牙连接失败，请重试"',
															icon: 'none'
														});
													}
												}
											}
										})
									}
								})



							}
						})
					}
				})
			},
			// ArrayBuffer转16进度字符串示例
			ab2hex(buffer) {
				const hexArr = Array.prototype.map.call(
					new Uint8Array(buffer),
					function(bit) {
						return ('00' + bit.toString(16)).slice(-2)
					}
				)
				return hexArr.join('')
			},
			setmtu() {
				var _this = this
				uni.setBLEMTU({
					deviceId: _this.alarmMac,
					mtu: _this.mtunum, // 设置MTU值为200字节
					success: function(e) {
						_this.connectNum = 0
						console.log("设置蓝牙最大传输单元成功");
						_this.writeBlueBefore()
					},
					fail: function(e) {
						if (_this.connectNum < 3) {
							console.log('连接失败，重新连接')
							_this.connectNum++
							_this.setmtu()
						} else {
							_this.loading = false
							_this.typetext = '设置蓝牙最大传输单元失败'
							_this.$refs.uToast.show({
								type: 'default',
								message: "设置蓝牙最大传输单元失败",
							})
						}
						console.log(e)
						console.log("设置蓝牙最大传输单元失败");
					}
				});
			},
			//蓝牙建立连接
			connectBlue() {
				var _this = this
				uni.createBLEConnection({
					// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
					deviceId: _this.alarmMac,
					timeout: 6000,
					success(res) {
						_this.connectNum = 0
						console.log('连接成功', res)
						setTimeout(() => {
							_this.setmtu()
						}, 1000)
					},
					fail(err) {
						console.log(err)
						if (err.errCode == -1) return
						if (_this.connectNum < 3) {
							console.log('连接失败，重新连接')
							_this.connectNum++
							_this.connectBlue()
						} else {
							_this.loading = false
							_this.typetext = '蓝牙连接失败，请重试'
							uni.showToast({
								title: '蓝牙连接失败，请重试"',
								icon: 'none'
							});
						}
					}
				})
			},
			//开始配对
			startClient() {
				this.timers.forEach(timerId => clearTimeout(timerId))
				this.timers = []
				if (this.loading) return
				if (!this.alarmName || !this.devName) {
					return uni.showToast({
						title: '请先输入设备编号',
						icon: 'none'
					});
				}
				this.alarmMac = ''
				this.devMac = ''
				this.alarmBlueData = {}
				this.devBlueData = {}

				var _this = this

				uni.closeBluetoothAdapter({
					success(res) {
						console.log('释放蓝牙连接', res)
						uni.openBluetoothAdapter({
							success: (res) => {
								console.log("初始化蓝牙模块", res)
								_this.startBlue()
							},
							fail: (err) => {
								console.log("初始化蓝牙模块err", err)
								if (err.errCode == 10001) {
									uni.showToast({
										title: '请先打开蓝牙',
										icon: 'none'
									});
								}
							}
						})
					}
				})

			},
			//开始蓝牙扫描
			startBlue() {
				console.log("初始化蓝牙设备startBlue")
				let _this = this
				uni.authorize({
					scope: 'scope.userLocation',
					success(res) {
						uni.getSystemInfo({
							success: function(res) {
								if (res.system.split(' ')[0] != 'iOS') {
									uni.startBluetoothDevicesDiscovery({
										success: (res) => {
											console.log("搜索蓝牙外围设备111", res)
											_this.typetext = '设备连接中...'
											_this.loading = true
											// uni.getBluetoothDevices({
											//   success(res) {
											//     console.log(res)
											//   }
											// })

											//监听搜索到的蓝牙设备
											let times = setTimeout(() => {
												if (_this.alarmMac && _this
													.devMac) {
													//已经成功扫描到了
												} else {
													_this.loading = false
													_this.typetext =
														'未扫描到对应设备，配对失败'
													uni.showToast({
														title: '未扫描到对应设备，配对失败',
														icon: 'none'
													});
													//搜寻结束，停止搜索蓝牙
													uni.stopBluetoothDevicesDiscovery({
														success(res) {
															console.log(
																'停止蓝牙搜索',
																res
															)
														}
													})
												}
											}, 30000)
											_this.timers.push(times)
											uni.onBluetoothDeviceFound(({
												devices: devices
											}) => {
												console.log(
													'onBluetoothDeviceFound',
													devices[0])
												if (devices[0].name.includes(
														_this.alarmName.slice(-10)
													)) {
													_this.alarmMac = devices[0]
														.deviceId
													_this.alarmBlueData = devices[
														0]
												}
												if (devices[0].name.includes(
														_this.devName.slice(-6))) {
													_this.devMac = devices[0]
														.deviceId
													_this.devBlueData = devices[0]
												}
												if (_this.alarmMac && _this
													.devMac) {
													//搜寻结束，停止搜索蓝牙
													uni.stopBluetoothDevicesDiscovery({
														success(res) {
															console.log(
																'停止蓝牙搜索',
																res
															)
														}
													})

													wx.onBLEMTUChange(function(
														res) {
														console.log(
															'bluetooth mtu is',
															res.mtu)
														_this.mtunum = res
															.mtu
														if (res.mtu <
															240) {
															_this.loading =
																false
															_this
																.typetext =
																'设置蓝牙传输单元失败，请更换设备重试'
															uni.showToast({
																title: '设置蓝牙传输单元失败，请更换设备重试"',
																icon: 'none'
															});
														}
													})
													//连接报警器蓝牙
													_this.connectNum = 0
													_this.connectBlue()
												}
												// this.getBlue()
											})
										},
										fail: (err) => {
											console.log("请开启手机定位服务")
											// _this.showToast('请开启手机定位服务')
											uni.showToast({
												title: '请开启手机定位服务',
												icon: 'none'
											});
										}
									})
								} else {
									console.log("IOS系统暂不支持此功能")
									// _this.showToast('IOS系统暂不支持此功能')
									uni.showToast({
										title: 'IOS系统暂不支持此功能',
										icon: 'none'
									});
								}
							}
						});
					},
					fail(err) {}
				})
			},
			//扫码
			scandev(type) {
				var _this = this
				uni.scanCode({
					success: function(res) {
						console.log(res)
						console.log('条码类型：' + res.scanType);
						console.log('条码内容：' + res.result);
						if (type == 1) {
							_this.getAlarmImei(res.result)
							// this.alarmName = res.result
						} else {
							_this.devName = res.result
							_this.devMac = ''
						}

					}
				});
			},
			//通过上壳号获取IMEI
			async getAlarmImei(uppernum) {
				const reg = /cce(.*)/g
				var result = reg.exec(uppernum)
				var imei = ''
				if (result) {
					imei = result[1]
				}
				var _this = this
				var res = await myrequestUrl({
					url: `https://oda.chichengcloud.cn/oda/dvcCodeSearchByOda/search/${imei}`,
					method: 'get'
				})
				if (res.code == 0) {
					let devData = res.data
					_this.alarmName = devData.modNum
					_this.alarmMac = ''
				} else {
					console.log(1111)
					uni.showToast({
						title: '获取IMEI失败',
						icon: 'none'
					});
				}
				// this.$request({
				// 	url: `http://106.14.172.215:9180/odb/dvcCodeSearch/search/${imei}`,
				// 	method: "Get",
				// 	dataType: 'json',
				// 	success: (res) => {
				// 		let devData = res.data.data
				// 		_this.alarmName = devData.modNum
				// 		_this.alarmMac = ''
				// 	},
				// 	fail: (err) => {
				// 		console.log("123");
				// 		reject()
				// 	}
				// }, false)
			},
			back() {
				uni.navigateBack();
			}
		},
		
		onUnload() {
			this.timers.forEach(timerId => clearTimeout(timerId))
			this.timers = []
			// 页面卸载时停止蓝牙搜索
			uni.closeBluetoothAdapter();

			// 移除蓝牙设备发现监听
			uni.offBluetoothDeviceFound();

			// 移除特征值变化监听
			uni.offBLECharacteristicValueChange();
		}
	}
</script>

<style lang="scss" scoped>
	::v-deep .primaryBtn {
		height: 90rpx;
		line-height: 90rpx;
		margin-top: 4px;
	}

	.top {
		display: flex;
		justify-content: space-between;
		padding: 10px 0;

		.top_item {
			text-align: center;

			.topnum {
				color: #3C9CFF;
				font-size: 25px;
				font-weight: 600;
			}
		}
	}

	.searchbox {
		display: flex;
		align-items: center;
		margin-top: 10px;
	}
</style>